home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / ctutor.zip / CHAP5.TXT < prev    next >
Text File  |  1987-07-04  |  35KB  |  784 lines

  1.             Chapter 5 - Functions, variables, and prototypes
  2.  
  3.  
  4.                       OUR FIRST USER DEFINED FUNCTION
  5.  
  6.              Load  and examine the file SUMSQRES.C for an example of
  7.         a C program with functions.   Actually this is not the first
  8.         function  we have encountered because the "main" program  we
  9.         have been using all along is technically a function,  as  is
  10.         the  "printf" function.   The "printf" function is a library
  11.         function that was supplied with your compiler.
  12.  
  13.              Notice the executable part of this program.   It begins
  14.         with a line that simply says "header()", which is the way to
  15.         call any function.  The parentheses are required because the
  16.         C compiler uses them to determine that it is a function call
  17.         and not simply a misplaced variable.  When the program comes
  18.         to this line of code, the function named "header" is called,
  19.         its  statements  are executed,  and control returns  to  the
  20.         statement  following this call.   Continuing on we come to a
  21.         "for"  loop which will be executed 7 times and  which  calls
  22.         another  function named "square" each time through the loop,
  23.         and  finally  a function named "ending" will be  called  and
  24.         executed.    For  the  moment  ignore  the  "index"  in  the
  25.         parentheses of the call to "square".  We have seen that this
  26.         program  therefore calls a header,  7 square calls,  and  an
  27.         ending. Now we need to define the functions.
  28.  
  29.                            DEFINING THE FUNCTIONS
  30.  
  31.              Following the main program you will see another program
  32.         that  follows all of the rules set forth so far for a "main"
  33.         program  except that it is named "header()".   This  is  the
  34.         function which is called from within the main program.  Each
  35.         of  these  statements are executed,  and when they  are  all
  36.         complete, control returns to the main program.
  37.  
  38.              The  first  statement sets the variable "sum" equal  to
  39.         zero because we will use it to accumulate a sum of  squares.
  40.         Since  the  variable  "sum" is defined as  an  integer  type
  41.         variable  prior to the main program,  it is available to  be
  42.         used  in  any of the following functions.   It is  called  a
  43.         "global" variable,  and it's scope is the entire program and
  44.         all  functions.   More  will  be  said about  the  scope  of
  45.         variables near the end of this chapter.  The next  statement
  46.         outputs  a header message to the monitor.   Program  control
  47.         then  returns  to  the  main  program  since  there  are  no
  48.         additional statements to execute in this function.
  49.  
  50.              It should be clear to you that the two executable lines
  51.         from  this  function  could be moved to  the  main  program,
  52.         replacing the header call,  and the program would do exactly
  53.         the same thing that it does as it is now written.  This does
  54.         not minimize the value of functions,  it merely  illustrates
  55.  
  56.  
  57.                                   Page 31
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.             Chapter 5 - Functions, variables, and prototypes
  68.  
  69.  
  70.         the operation of this simple function in a simple way.   You
  71.         will find functions to be very valuable in C programming.
  72.  
  73.                        PASSING A VALUE TO A FUNCTION
  74.  
  75.              Going  back  to the main program,  and the  "for"  loop
  76.         specifically,  we find the new construct from the end of the
  77.         last lesson used in the last part of the "for" loop,  namely
  78.         the  "index++".  You should get used to seeing this, as  you
  79.         will see it a lot in C programs.
  80.  
  81.              In the call to the function "square",  we have an added
  82.         feature, namely the variable "index" within the parentheses.
  83.         This  is  an indication to the compiler that when you go  to
  84.         the function,  you wish to take along the value of index  to
  85.         use in the execution of that function.  Looking ahead at the
  86.         function  "square",  we  find that another variable name  is
  87.         enclosed in its parentheses,  namely the variable  "number".
  88.         This  is  the name we prefer to call the variable passed  to
  89.         the  function when we are in the function.   We can call  it
  90.         anything  we wish as long as it follows the rules of  naming
  91.         an identifier.   Since the function must know what type  the
  92.         variable  is,  it is defined following the function name but
  93.         before the opening brace of the function itself.   Thus, the
  94.         line  containing "int number;" tells the function  that  the
  95.         value  passed to it will be an integer type variable.   With
  96.         all of that out of the way,  we now have the value of  index
  97.         from  the main program passed to the function "square",  but
  98.         renamed "number", and available for use within the function.
  99.         This  is the "classic" style of defining function  variables
  100.         and has been in use since C was originally defined.  A newer
  101.         method is gaining in popularity due to its many benefits and
  102.         will be discussed later in this chapter.
  103.  
  104.              Following the opening brace of the function,  we define
  105.         another  variable "numsq" for use only within  the  function
  106.         itself,  (more  about  that  later)  and  proceed  with  the
  107.         required  calculations.   We set "numsq" equal to the square
  108.         of  number,  then add numsq to the current total  stored  in
  109.         "sum".   Remember  that "sum += numsq" is the same as "sum =
  110.         sum + numsq" from the last lesson.   We print the number and
  111.         its square, and return to the main program.
  112.  
  113.                   MORE ABOUT PASSING A VALUE TO A FUNCTION
  114.  
  115.              When we passed the value of "index" to the function,  a
  116.         little  more  happened  than meets  the  eye.   We  did  not
  117.         actually  pass  the  value  of index  to  the  function,  we
  118.         actually  passed  a  copy of the value.   In  this  way  the
  119.         original value is protected from accidental corruption by  a
  120.         called  function.   We  could  have  modified  the  variable
  121.  
  122.  
  123.                                   Page 32
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.             Chapter 5 - Functions, variables, and prototypes
  134.  
  135.  
  136.         "number" in any way we wished in the function "square",  and
  137.         when we returned to the main program, "index" would not have
  138.         been  modified.   We thus protect the value of a variable in
  139.         the main program from being accidentally corrupted,  but  we
  140.         cannot  return  a value to the main program from a  function
  141.         using this technique.  We will find a well defined method of
  142.         returning  values  to  the main program or  to  any  calling
  143.         function  when we get to arrays and another method  when  we
  144.         get  to pointers.   Until then the only way you will be able
  145.         to  communicate  back to the calling function will  be  with
  146.         global  variables.    We  have  already  hinted  at   global
  147.         variables  above,  and will discuss them in detail later  in
  148.         this chapter.
  149.  
  150.              Continuing  in  the main program,  we come to the  last
  151.         function call, the call to "ending".  This call simply calls
  152.         the last function which has no local variables defined.   It
  153.         prints out a message with the value of "sum" contained in it
  154.         to  end the program.   The program ends by returning to  the
  155.         main  program and finding nothing else to do.   Compile  and
  156.         run this program and observe the output.
  157.  
  158.                         NOW TO CONFESS A LITTLE LIE
  159.  
  160.              I told you a short time ago that the only way to get  a
  161.         value  back to the main program was through use of a  global
  162.         variable,  but  there  is another way which we will  discuss
  163.         after  you load and display the file  named  SQUARES.C.   In
  164.         this  file we will see that it is simple to return a  single
  165.         value  from a called function to the calling function.   But
  166.         once again,  it is true that to return more than one  value,
  167.         we will need to study either arrays or pointers.
  168.  
  169.              In the main program, we define two integers and begin a
  170.         "for"  loop  which  will be executed  8  times.   The  first
  171.         statement of the "for" loop is "y = squ(x);", which is a new
  172.         and rather strange looking construct.  From past experience,
  173.         we  should have no trouble understanding that  the  "squ(x)"
  174.         portion  of  the statement is a call to the  "squ"  function
  175.         taking along the value of "x" as a variable.  Looking  ahead
  176.         to the function itself we find that the function prefers  to
  177.         call  the variable "in" and it proceeds to square the  value
  178.         of  "in" and call the result "square".  Finally, a new  kind
  179.         of  a statement appears, the "return" statement.  The  value
  180.         within  the parentheses is assigned to the  function  itself
  181.         and  is  returned  as a usable value in  the  main  program.
  182.         Thus,  the function call "squ(x)" is assigned the  value  of
  183.         the square and returned to the main program such that "y" is
  184.         then  set  equal  to  that value.   If  "x"  were  therefore
  185.         assigned  the value 4 prior to this call, "y" would then  be
  186.         set to 16 as a result of this line of code.
  187.  
  188.  
  189.                                   Page 33
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.             Chapter 5 - Functions, variables, and prototypes
  200.  
  201.  
  202.  
  203.              Another  way  to  think  of this  is  to  consider  the
  204.         grouping  of characters "squ(x)" as another variable with  a
  205.         value  that is the square of "x",  and this new variable can
  206.         be used any place it is legal to use a variable of its type.
  207.         The values of "x" and "y" are then printed out.
  208.  
  209.              To  illustrate  that the grouping of  "squ(x)"  can  be
  210.         thought  of as just another variable,  another "for" loop is
  211.         introduced in which the function call is placed in the print
  212.         statement rather than assigning it to a new variable.
  213.  
  214.              One  last  point must be made,  the  type  of  variable
  215.         returned must be defined in order to make sense of the data,
  216.         but the compiler will default the type to integer if none is
  217.         specified.   If  any  other  type is  desired,  it  must  be
  218.         explicitly defined.   How to do this will be demonstrated in
  219.         the next example program.
  220.  
  221.              Compile  and  run  this program  which  also  uses  the
  222.         "classic" method of defining function variables.
  223.  
  224.                             FLOATING POINT FUNCTIONS
  225.  
  226.              Load the program FLOATSQ.C for an example of a function
  227.         with a floating point type of return.  It begins by defining
  228.         a global floating point variable we will use later.  Then in
  229.         the  "main"  part  of the program,  an integer  is  defined,
  230.         followed  by two floating point variables,  and then by  two
  231.         strange  looking definitions.   The expressions "sqr()"  and
  232.         "glsqr()"  look like function calls and they are.   This  is
  233.         the proper way in C to define that a function will return  a
  234.         value that is not of the type "int", but of some other type,
  235.         in  this case "float".   This tells the compiler that when a
  236.         value  is returned from either of these  two  functions,  it
  237.         will be of type "float".  This is, once again, the "classic"
  238.         method of defining functions.
  239.  
  240.              Now  refer to the function "sqr" near the center of the
  241.         listing and you will see that the function name is  preceded
  242.         by the name "float".   This is an indication to the compiler
  243.         that  this  function will return a value of type "float"  to
  244.         any program that calls it.   The function is now  compatible
  245.         with  the call to it.   The line following the function name
  246.         contains  "float inval;",  which indicates to  the  compiler
  247.         that  the variable passed to this function from the  calling
  248.         program will be of type "float".
  249.  
  250.              The next function,  namely "glsqr",  will also return a
  251.         "float"  type  variable,  but it uses a global variable  for
  252.         input.   It  also does the squaring right within the  return
  253.  
  254.  
  255.                                   Page 34
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.             Chapter 5 - Functions, variables, and prototypes
  266.  
  267.  
  268.         statement  and  therefore has no need to define  a  separate
  269.         variable to store the product.
  270.  
  271.              The  overall structure of this program should  pose  no
  272.         problem and will not be discussed in any further detail.  As
  273.         is customary with all example programs, compile and run this
  274.         program.
  275.  
  276.                THERE IS A BUG IN THE FIRST VERSION OF TURBO C
  277.  
  278.              When  you run this program, using version 1.0 of  Turbo
  279.         C, you will find that the function named "sqr()" will return
  280.         a  value of zero.  This is because it receives a  zero  from
  281.         the  calling program.  The program is written correctly  but
  282.         the compiler has a bug in it.  A call to Borland resulted in
  283.         a  fix  by  simply using the  "modern"  method  of  function
  284.         definition rather than the "classic" which has been used  to
  285.         this  point.   As  you  read articles on  C,  you  will  see
  286.         programs  written in the "classic" style, so you need to  be
  287.         capable  of reading them.  It would be  highly  recommended,
  288.         however,  that you learn and use the "modern"  method  which
  289.         will be covered shortly in this tutorial.  We will return to
  290.         this program and show how to fix it so that it will work  in
  291.         spite of the bug.
  292.  
  293.              There  will  be times that you will have a need  for  a
  294.         function   to   return  a  pointer  as  a  result  of   some
  295.         calculation.  There is a way to define a function so that it
  296.         does  just that.   We haven't studied pointers yet,  but  we
  297.         will soon.  This is just a short preview of things to come.
  298.  
  299.                              SCOPE OF VARIABLES
  300.  
  301.              Load the next program,  SCOPE.C,  and display it for  a
  302.         discussion  of the scope of variables in a program.   Ignore
  303.         the 4 statements in lines 2 through 5 of this program for  a
  304.         few moments, and we will discuss them later.
  305.  
  306.              The first variable defined is a global variable "count"
  307.         which  is available to any function in the program since  it
  308.         is defined before any of the functions.   In addition, it is
  309.         always  available  because  it does not come and go  as  the
  310.         program  is  executed.   (That  will  make  sense  shortly.)
  311.         Farther down in the program,  another global variable  named
  312.         "counter"  is  defined  which  is also  global  but  is  not
  313.         available  to the main program since it is defined following
  314.         the main program.  A global variable is any variable that is
  315.         defined  outside of any function.   Note that both of  these
  316.         variables  are sometimes referred to as  external  variables
  317.         because they are external to any functions.
  318.  
  319.  
  320.  
  321.                                   Page 35
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.             Chapter 5 - Functions, variables, and prototypes
  332.  
  333.  
  334.              Return  to  the  main  program and  you  will  see  the
  335.         variable  "index"  defined as an integer.   Ignore the  word
  336.         "register" for the moment.   This variable is only available
  337.         within the main program because that is where it is defined.
  338.         In addition, it is an "automatic" variable, which means that
  339.         it  only comes into existence when the function in which  it
  340.         is  contained  is  invoked,  and ceases to  exist  when  the
  341.         function  is  finished.   This  really  means  nothing  here
  342.         because the main program is always in operation,  even  when
  343.         it  gives  control to another function.  Another integer  is
  344.         defined  within  the  "for"  braces,  namely  "stuff".   Any
  345.         pairing  of braces can contain a variable  definition  which
  346.         will  be  valid  and  available only while  the  program  is
  347.         executing statements within those braces.  The variable will
  348.         be  an  "automatic" variable and will cease  to  exist  when
  349.         execution leaves the braces.   This is convenient to use for
  350.         a loop counter or some other very localized variable.
  351.  
  352.                        MORE ON "AUTOMATIC" VARIABLES
  353.  
  354.              Observe  the  function named "head1" in line  26  which
  355.         looks  a  little funny because of "void" being  used  twice.
  356.         The purpose of the uses of the word "void" will be explained
  357.         shortly.  The  function contains a variable  named  "index",
  358.         which  has  nothing  to  do with the  "index"  of  the  main
  359.         program, except that both are automatic variables.  When the
  360.         program  is  not  actually  executing  statements  in   this
  361.         function,  this variable named "index" does not even  exist.
  362.         When "head1" is called, the variable is generated, and  when
  363.         "head1"   completes  its  task,  the  variable  "index"   is
  364.         eliminated completely from existence.  Keep in mind  however
  365.         that  this does not affect the variable of the same name  in
  366.         the main program, since it is a completely separate entity.
  367.  
  368.              Automatic   variables  therefore,   are   automatically
  369.         generated and disposed of when needed.   The important thing
  370.         to remember is that from one call to a function to the  next
  371.         call,  the  value of an automatic variable is not  preserved
  372.         and must therefore be reinitialized.
  373.  
  374.                          WHAT ARE STATIC VARIABLES?
  375.  
  376.              An  additional variable type must be mentioned at  this
  377.         point,  the "static" variable.  By putting the reserved word
  378.         "static"  in  front  of  a  variable  declaration  within  a
  379.         function,  the variable or variables in that declaration are
  380.         static  variables  and will stay in existence from  call  to
  381.         call  of  the  particular function.
  382.  
  383.              By  putting  the  same reserved word  in  front  of  an
  384.         external variable, one outside of any function, it makes the
  385.  
  386.  
  387.                                   Page 36
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.             Chapter 5 - Functions, variables, and prototypes
  398.  
  399.  
  400.         variable  private  and  not accessible to use in  any  other
  401.         file.  This implies that it is possible to refer to external
  402.         variables  in other separately compiled files,  and that  is
  403.         true.  Examples of this usage will be given in chapter 14 of
  404.         this tutorial.
  405.  
  406.                          USING THE SAME NAME AGAIN
  407.  
  408.              Refer  to  the  function named  "head2".   It  contains
  409.         another  definition  of the variable  named  "count".   Even
  410.         though  "count"  has  already  been  defined  as  a   global
  411.         variable,  it  is  perfectly all right to reuse the name  in
  412.         this  function.   It is a completely new variable  that  has
  413.         nothing to do with the global variable of the same name, and
  414.         causes  the  global  variable  to  be  unavailable  in  this
  415.         function.   This allows you to write programs using existing
  416.         functions  without worrying about what names were  used  for
  417.         variables in the functions because there can be no conflict.
  418.         You  only  need to worry about the variables that  interface
  419.         with the functions.
  420.  
  421.                         WHAT IS A REGISTER VARIABLE?
  422.  
  423.              Now  to  fulfill  a promise made earlier about  what  a
  424.         register  variable  is.   A  computer can  keep  data  in  a
  425.         register  or  in  memory.   A  register is  much  faster  in
  426.         operation  than  memory  but there are  very  few  registers
  427.         available for the programmer to use.   If there are  certain
  428.         variables  that are used extensively in a program,  you  can
  429.         designate  that  those  variables  are to  be  stored  in  a
  430.         register  if possible in order to speed up the execution  of
  431.         the program.  Turbo C allows you to use 2 register variables
  432.         and will ignore additional requests if you request more than
  433.         2.
  434.  
  435.              Turbo C allows you to use register variables for  short
  436.         int and int types along with their unsigned counterparts. It
  437.         also  allows  you  to  use register  storage  for  two  byte
  438.         pointers which we will study in about 3 chapters.
  439.  
  440.                         WHERE DO I DEFINE VARIABLES?
  441.  
  442.              Now for a refinement on a general rule stated  earlier.
  443.         When  you have variables brought to a function as  arguments
  444.         to  the function, and you are using the "classic"  style  of
  445.         programming, they are defined immediately after the function
  446.         name and prior to the opening brace for the program.   Other
  447.         variables used in the function are defined at the  beginning
  448.         of the function, immediately following the opening brace  of
  449.         the function, and before any executable statements.
  450.  
  451.  
  452.  
  453.                                   Page 37
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.             Chapter 5 - Functions, variables, and prototypes
  464.  
  465.  
  466.                             WHAT IS PROTOTYPING?
  467.  
  468.              A  prototype  is a "model" of the real thing  and  when
  469.         programming  in  Turbo C, you have the ability to  define  a
  470.         "model" of each function for the compiler.  The compiler can
  471.         then  use  the "model" to check each of your  calls  to  the
  472.         function  and determine if you have used the correct  number
  473.         of  arguments  in the function call and if they are  of  the
  474.         correct type.  By using prototypes, you let the compiler  do
  475.         some  additional error checking for you.  The ANSI  standard
  476.         for  C  should be released late in 1987,  and  will  contain
  477.         prototyping as part of its recommended standard.  Every good
  478.         C  compiler will have prototyping available, so  you  should
  479.         learn to use it.
  480.  
  481.              Returning to lines 3, 4, and 5 in SCOPE.C, we have  the
  482.         prototypes  for  the three functions  contained  within  the
  483.         program.   The first "void" in each line tells the  compiler
  484.         that  these particular functions do not return a  value,  so
  485.         that the compiler would flag the statement index =  head1();
  486.         as  an  error because nothing is returned to assign  to  the
  487.         variable  "index".  The word "void" within  the  parentheses
  488.         tells the compiler that this function requires no parameters
  489.         and  if a variable were included, it would be an  error  and
  490.         the  compiler would issue a warning message.  If  you  wrote
  491.         the  statement  head1(index);, it would be  a  error.   This
  492.         allows  you  to use type checking when programming in  C  in
  493.         much the same manner that it is used in Pascal, Modula 2, or
  494.         Ada.
  495.  
  496.              You  should enable prototype checking with Turbo  C  at
  497.         this  point to see how well it works.  In the "Option"  menu
  498.         select  "Compiler", then "Errors", followed by "Less  common
  499.         errors",  then  turn  on  "F:  Call  to  function  with   no
  500.         prototype" by hitting the "F" key.  The program SCOPE.C will
  501.         not produce any warnings, but RECURSON.C which is next  will
  502.         result in some warning messages.
  503.  
  504.              Line 2 of SCOPE.C tells the system to go to the include
  505.         files  and  get the file named STDIO.H  which  contains  the
  506.         prototypes  for the standard input and output  functions  so
  507.         they can be checked for proper variable types.  Don't  worry
  508.         about the "include" yet, it will be covered in detail  later
  509.         in this tutorial.
  510.  
  511.              Compile  and run this program and, when you  return  to
  512.         the  Integrated  Environment,  observe  that  there  are  no
  513.         warning messages.
  514.  
  515.  
  516.  
  517.  
  518.  
  519.                                   Page 38
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.             Chapter 5 - Functions, variables, and prototypes
  530.  
  531.  
  532.                          STANDARD FUNCTION LIBRARIES
  533.  
  534.              Every  compiler  comes  with some  standard  predefined
  535.         functions  which  are available for  your  use.   These  are
  536.         mostly   input/output   functions,   character  and   string
  537.         manipulation  functions, and math functions.  We will  cover
  538.         most  of  these  in  subsequent  chapters.   Prototypes  are
  539.         defined  for you by the Turbo C compiler writer for  all  of
  540.         the  functions that are included with your compiler.  A  few
  541.         minutes  spent  studying your Turbo C Reference  Guide  will
  542.         give you an insight in where the prototypes are defined  for
  543.         each of the functions.
  544.  
  545.              In addition,  most compilers have additional  functions
  546.         predefined that are not standard but allow the programmer to
  547.         get the most out of his particular computer.  In the case of
  548.         the  IBM-PC and compatibles,  most of these functions  allow
  549.         the  programmer  to use the BIOS services available  in  the
  550.         operating system, or to write directly to the video  monitor
  551.         or to any place in memory.  These will not be covered in any
  552.         detail as you will be able to study these unique aspects  of
  553.         the  Turbo  C  compiler on your own.   The  entire  Turbo  C
  554.         Reference Guide is devoted to defining functions.   Many  of
  555.         these kinds of functions are used in the example programs in
  556.         chapter 14.
  557.  
  558.  
  559.                              WHAT IS RECURSION?
  560.  
  561.              Recursion  is another of those  programming  techniques
  562.         that  seem very intimidating the first time you come  across
  563.         it,  but  if  you will load and display the example  program
  564.         named RECURSON.C, we will take all of the mystery out of it.
  565.         This  is probably the simplest recursive program that it  is
  566.         possible  to write and it is therefore a stupid  program  in
  567.         actual  practice,  but for purposes of illustration,  it  is
  568.         excellent.
  569.  
  570.              Recursion  is  nothing more than a function that  calls
  571.         itself.   It is therefore in a loop which must have a way of
  572.         terminating.   In the program on your monitor,  the variable
  573.         "index"  is  set to 8,  and is used as the argument  to  the
  574.         function  "count_dn".   The function simply  decrements  the
  575.         variable, prints it out in a message, and if the variable is
  576.         not  zero,  it calls itself,  where it decrements it  again,
  577.         prints it,  etc.  etc. etc. Finally, the variable will reach
  578.         zero,  and the function will not call itself again. Instead,
  579.         it  will  return  to the prior time it  called  itself,  and
  580.         return  again,  until  finally it will return  to  the  main
  581.         program and will return to DOS.
  582.  
  583.  
  584.  
  585.                                   Page 39
  586.  
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.             Chapter 5 - Functions, variables, and prototypes
  596.  
  597.  
  598.              For  purposes  of understanding you can think of it  as
  599.         having 8 copies of the function "count_dn" available and  it
  600.         simply  called all of them one at a time,  keeping track  of
  601.         which  copy it was in at any given time.   That is not  what
  602.         actually  happened,  but it is a reasonable illustration for
  603.         you to begin understanding what it was really doing.
  604.  
  605.                               WHAT DID IT DO?
  606.  
  607.              A  better explanation of what actually happened  is  in
  608.         order.   When you called the function from itself, it stored
  609.         all  of the variables and all of the internal flags it needs
  610.         to  complete the function in a block  somewhere.   The  next
  611.         time it called itself,  it did the same thing,  creating and
  612.         storing  another  block of everything it needed to  complete
  613.         that  function call.   It continued making these blocks  and
  614.         storing them away until it reached the last function when it
  615.         started  retrieving the blocks of data,  and using  them  to
  616.         complete  each function call.   The blocks were stored on an
  617.         internal part of the computer called the "stack".  This is a
  618.         part  of  memory carefully organized to store data  just  as
  619.         described above.  It is beyond the scope of this tutorial to
  620.         describe the stack in detail,  but it would be good for your
  621.         programming  experience to read some material describing the
  622.         stack.   A stack is used in nearly all modern computers  for
  623.         internal housekeeping chores.
  624.  
  625.              In using recursion,  you may desire to write a  program
  626.         with  indirect recursion as opposed to the direct  recursion
  627.         described  above.    Indirect  recursion  would  be  when  a
  628.         function  "A"  calls the function "B",  which in turn  calls
  629.         "A",  etc.   This is entirely permissible,  the system  will
  630.         take  care of putting the necessary things on the stack  and
  631.         retrieving  them when needed again.   There is no reason why
  632.         you  could not have three functions calling each other in  a
  633.         circle,  or four,  or five,  etc.   The C compiler will take
  634.         care of all of the details for you.
  635.  
  636.              The thing you must remember about recursion is that  at
  637.         some  point,  something  must  go to  zero,  or  reach  some
  638.         predefined  point to terminate the loop.   If not,  you will
  639.         have  an  infinite  loop,  and the stack will  fill  up  and
  640.         overflow,  giving  you  an error and  stopping  the  program
  641.         rather abruptly.
  642.  
  643.              Compile  and  run  this  program.   If  you  left   the
  644.         prototype  checking on from the last program you  will  find
  645.         several  "warnings"  in the message window when  you  return
  646.         to  the  Integrated  Environment.  Study  these  for  a  few
  647.         minutes.  One of the suggested exercises at the end of  this
  648.  
  649.  
  650.  
  651.                                   Page 40
  652.  
  653.  
  654.  
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661.             Chapter 5 - Functions, variables, and prototypes
  662.  
  663.  
  664.         chapter is to modify this program to eliminate the prototype
  665.         warnings.
  666.  
  667.                         ANOTHER EXAMPLE OF RECURSION
  668.  
  669.              The  program  named  BACKWARD.C is another  example  of
  670.         recursion,  so load it and display it on your screen.   This
  671.         program  is  similar to the last one except that it  uses  a
  672.         character array.  Each successive call to the function named
  673.         "forward_and_backwards" causes one character of the  message
  674.         to  be printed.  Additionally, each time the function  ends,
  675.         one of the characters is printed again, this time  backwards
  676.         as the string of recursive function calls is retraced.
  677.  
  678.              This  program  uses  the "modern"  method  of  function
  679.         definition  and  includes full prototype  definitions.   The
  680.         "modern"  method of function definition moves the  types  of
  681.         the  variables into the parentheses along with the  variable
  682.         names  themselves.   The  final  result  is  that  the  line
  683.         containing   the   function  name  looks   more   like   the
  684.         corresponding line in Pascal, Modula 2, or Ada.
  685.  
  686.              Don't worry about the character array defined in line 9
  687.         or  the  other  new  material  presented  here.   After  you
  688.         complete chapter 7 of this tutorial,  this program will make
  689.         sense.   It  was  felt that introducing a second example  of
  690.         recursion was important so this file is included here.
  691.  
  692.              Compile  and run this program with  prototype  checking
  693.         on, and observe the results.
  694.  
  695.                  HOW TO WORK AROUND THE TURBO C (v1.0) BUG
  696.  
  697.              Load and display the program named FLOATSQ2.C which  is
  698.         an  exact copy of the program FLOATSQ.C which we  considered
  699.         earlier with prototyping added.  The return of the erroneous
  700.         zeros  is now repaired and the correct values are  returned.
  701.         Apparently,  when  the  coders of Turbo C  at  Borland  were
  702.         testing this compiler, they used prototyping and never found
  703.         this bug.  The use of prototyping is a good practice for all
  704.         C programmers to get into.
  705.  
  706.              Several things should be mentioned about this  program.
  707.         First, the word "float" at the beginning of lines 27 and  35
  708.         indicate to the compiler that these functions are  functions
  709.         that return "float" type values.  Also, since the prototypes
  710.         are  given before "main", the functions are not required  to
  711.         be identified in line 12 as they were in line 7 of FLOATSQ.C
  712.         earlier  in this chapter.  They can be included in line  12,
  713.         but they are not required to be.
  714.  
  715.  
  716.  
  717.                                   Page 41
  718.  
  719.  
  720.  
  721.  
  722.  
  723.  
  724.  
  725.  
  726.  
  727.             Chapter 5 - Functions, variables, and prototypes
  728.  
  729.  
  730.              Notice  also that the type of the variable  "inval"  is
  731.         included  within  the parentheses in line 27.  It  would  be
  732.         very educational for you to modify this program so that  you
  733.         included  a  call  to "sqr" with a variable  of  type  "int"
  734.         within  the  parentheses to see what kind of a  warning  you
  735.         would  get.   Do  the  same thing  in  the  program  without
  736.         prototype checking, FLOATSQ.C.
  737.  
  738.         PROGRAMMING EXERCISES
  739.  
  740.         1.   Rewrite  TEMPCONV.C,  from an earlier chapter, and move
  741.              the temperature calculation to a function.
  742.  
  743.         2.   Write a program that writes your name on the monitor 10
  744.              times by calling a function to do the writing. Move the
  745.              called function ahead of the "main" function to see  if
  746.              the Turbo C compiler will allow it.
  747.  
  748.         3.   Add  prototyping  to the program  named  RECURSON.C  to
  749.              eliminate the warnings.
  750.  
  751.  
  752.  
  753.  
  754.  
  755.  
  756.  
  757.  
  758.  
  759.  
  760.  
  761.  
  762.  
  763.  
  764.  
  765.  
  766.  
  767.  
  768.  
  769.  
  770.  
  771.  
  772.  
  773.  
  774.  
  775.  
  776.  
  777.  
  778.  
  779.  
  780.  
  781.  
  782.  
  783.                                   Page 42
  784.